<template>
  <div class="login">
    <div class="login_box">
      <div class="login_form">
        <div class="login_form_item">
          <input class="input" v-model="loginForm.account" placeholder="请输入用户名"/>
        </div>
        <div class="login_form_item">
          <input class="input" v-model="loginForm.password" type="password" placeholder="请输入密码"/>
        </div>
        <div class="login_form_item">
          <button class="button" @click="login">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {onLogin} from "@/api";
import {Toast} from "vant";
import {clearToast, setLoading} from "@/utils";

export default {
  name: "index",
  data() {
    return {
      show: false,
      loginForm: {
        account: null,
        password: null
      }
    }
  },
  methods: {
    login() {
      const {account, password} = this.loginForm
      if (!account) return Toast('请输入用户名')
      if (!password) return Toast('请输入密码')
      setLoading('登录中...')
      onLogin(this.loginForm).then(res => {
        if (res.code === 200) {
          sessionStorage.setItem('token', res.data.token)
          sessionStorage.setItem('name', res.data.real_name)
          sessionStorage.setItem('show', res.data.show)
          sessionStorage.setItem('usergroupId', res.data.webuser.usergroupId)
          this.$router.push('/home')
          clearToast()
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login {
  background-image: url("../../assets/login_bg.jpg");
  background-size: 100% 100%;
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;

  .login_box {
    width: 100%;
    padding: 0 40px;
    box-sizing: border-box;
    //position: absolute;
    //top: 50%;
    //left: 50%;
    //transform: translate(-50%, -50%);

    .login_form {
      width: 100%;

      .login_form_item {
        margin-bottom: 20px;
        text-align: center;

        .input {
          width: 100%;
          height: 40px;
          border-radius: 6px;
          border: 1px solid #c3c5cb;
          font-size: 16px;
          padding: 0 15px;
          box-sizing: border-box;
        }

        .input:focus {
          border: 1px solid #4e96de;
          transition: all .2s;
        }

        .button {
          width: 100%;
          height: 40px;
          font-size: 16px;
          margin-top: 20px;
          background: #4896f5;
          border: 1px solid #4e96de;
          color: #ffffff;
          border-radius: 6px;
        }

        .button:active {
          background: #82ace8;
        }
      }
    }
  }
}
</style>
